﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.XPath;
using System.IO;
using System.Web.UI;

namespace Marbles.Core.Content.CDN
{
    public class CDNParser
    {

        public Dictionary<string, string> Urls { get { return _urls; } }

        private Dictionary<string, string> _urls = null;
        private Dictionary<string, string> _types = null;

        public CDNParser()
        {
            _urls = new Dictionary<string, string>();
            _types = new Dictionary<string, string>();
            Parse();
        }

        public void RegisterHeaderIncludes(Marbles.Core.Pages.Page page)
        {
            foreach (string key in this.Urls.Keys)
            {
                switch (_types[key].ToLower())
                {
                    case "text/javascript":
                        page.RegisterClientScriptHeaderInclude(key, Urls[key]);
                        break;
                    case "text/css":
                        page.AddStyleSheetInclude(key, Urls[key]);
                        break;
                }

            }

        }

        public void RegisterHeaderIncludes(System.Web.UI.Page page)
        {
            foreach (string key in this.Urls.Keys)
            {
                switch (_types[key].ToLower())
                {
                    case "text/javascript":
                        page.ClientScript.RegisterClientScriptInclude(key, Urls[key]);
                        break;
                    case "text/css":
                        LiteralControl control = new LiteralControl();
                        control.ID = key;
                        control.Text = string.Format("<link rel=\"Stylesheet\" type=\"text/css\" href=\"{0}\" />", Urls[key]);
                        page.Header.Controls.Add(control);
                        break;
                }

            }

        }


        public void Parse()
        {
            string _filename = HttpContext.Current.Server.MapPath("~/App_Data/CDN/urls.xml");
            if (!System.IO.File.Exists(_filename)) { return; }
            FileStream stream = new FileStream(_filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            XPathDocument doc = new XPathDocument(stream);
            XPathNavigator nav = doc.CreateNavigator();
            nav.MoveToRoot();
            nav.MoveToFirstChild();
            if (nav.Name == "cdns") { nav.MoveToFirstChild(); }
            _urls.Clear();
            _types.Clear();

            string root = string.Empty;
            do
            {
                if (nav.Name == "cdn")
                {
                    root = nav.GetAttribute("name", "");
                    nav.MoveToFirstChild();
                    do
                    {
                        if (nav.Name == "add")
                        {
                            if (!string.IsNullOrEmpty(nav.GetAttribute("default", "")))
                            {
                                if (Convert.ToBoolean(nav.GetAttribute("default", "")))
                                {
                                    _urls.Add(nav.GetAttribute("id", ""), nav.GetAttribute("url", ""));
                                    _types.Add(nav.GetAttribute("id", ""), nav.GetAttribute("type", ""));
                                }
                            }
                        }
                    } while (nav.MoveToNext());
                    nav.MoveToParent();
                }


            } while (nav.MoveToNext());

            stream.Close();
        }
    }
}